package cn.kattomy.shop.user.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.kattomy.shop.base.dao.BaseDao;
import cn.kattomy.shop.user.dao.UserDao;
import cn.kattomy.shop.user.vo.User;

public class UserDaoImpl extends BaseDao implements UserDao {
	
	@Override
	public User queryUserByUsername(String username) {
		String hql = "FROM User u WHERE u.username = ?";
		User user = (User) getSession().createQuery(hql).setString(0, username).uniqueResult();
		return user;
	}

	@Override
	public void saveOrUpdate(User user) {
		getSession().saveOrUpdate(user);
	}

	@Override
	public User queryUserByCode(String code) {
		String hql = "FROM User u WHERE u.code = ?";
		return (User) getSession().createQuery(hql).setString(0, code).uniqueResult();
	}

	@Override
	public User queryUserByMultiCondition(User user) {
		String hql = "FROM User u WHERE u.username = ? AND u.password = ? AND u.state = 1 ";
//		List<User> list = getSession().createQuery(hql).setString(0, user.getUsername()).setString(1, user.getPassword()).list();
//		if(list != null && list.size() > 0){
//			return list.get(0);
//		}
//		return null;
		return (User) getSession().createQuery(hql).setString(0, user.getUsername()).setString(1, user.getPassword()).uniqueResult();
	}

	@Override
	public List<User> findAll() {
		String hql = "From User u order by uid";
		List<User> list = getSession().createQuery(hql).list();
		return list;
	}

	@Override
	public User queryUserByMultiCondition2(User user) {
		String hql = "FROM User u WHERE u.email = ? AND u.password = ? AND u.state = 1 ";
		return (User) getSession().createQuery(hql).setString(0, user.getEmail()).setString(1, user.getPassword()).uniqueResult();
	}

	@Override
	public User queryUserByEmail(String email) {
		String hql = "FROM User u WHERE u.email = ?";
		User user = (User) getSession().createQuery(hql).setString(0, email).uniqueResult();
		return user;
	}

}
